
import {defineStore} from 'pinia';
import utils from '@/utils';
import router from '@/router';

const AccessTokenKey = 'authUser';

interface Imenu {
	id: number
	name: string
	url: string
}

interface State {
	id: number;
	name: string;
	sex: number;
	username: string;
	access: string;
	refresh: string;
	refresh_expire: string;
	menu: Imenu[],
	phone: string,
	email: string,
	no: string,
	status: number,
	role_id: number,
	role_name: string,
	organization_id: number,
	organization_name: string,
	organization_no: string,
	token: string,
}

export const useAuthUserStore = defineStore('authUserStore', {
	state: (): State =>
		utils.getStorage(AccessTokenKey, {
			id: 0,
			username: '',
			name: '',
			sex: 0,
			access: '',
			menu: [],
			refresh: '',
			refresh_expire: '',
			phone: '',
			email: '',
			no: '',
			status: 0,
			role_id: 0,
			role_name: '',
			organization_id: 0,
			organization_name: '',
			organization_no: '',
			token: '',
		}),
	getters: {
		isAuth: (state) => state.token !== ''
	},
	actions: {
		setUser(payload: State) {
			this.$patch(payload);
			utils.setStorage(AccessTokenKey, payload);
		},
		setToken(payload: string) {
			this.$patch({token: payload});
		},
		removeToken() {
			this.$patch({token: ''});
		},
		toLogin() {
			// router.push('/login');
			window.open('/student/Login','_self')
		},
		// 字段重置
		clearUser() {
			utils.setStorage(AccessTokenKey, {
				id: 0,
				username: '',
				name: '',
				sex: 0,
				access: '',
				menu: [],
				refresh: '',
				refresh_expire: '',
				phone: '',
				email: '',
				no: '',
				status: 0,
				role_id: 0,
				role_name: '',
				organization_id: 0,
				organization_name: '',
				organization_no: ''
			});
			this.$reset();
		},
	},
});

